﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SmallEnemyPlane : EnemyPlane {

	private float MAX_ROTATION = 10;// 每秒最大可旋转的角度
	private float max_rotation = 3f; // 每帧最大可旋转的角度
	private Vector3 targetEndPosition;// 飞机死亡前的位置
	private float timer = 0;//计时器
	

	// Use this for initialization
	void Start () {
		InitPro();
		hpSlider = this.GetComponent<HPSlider> ();
		isDeath = false;
		Spawn.SetTarget += SetTarget;
		distanceWeaponAttack = transform.Find("gun").GetComponent<EnemyGun>().GetWeapon().AttackDistance;
	}

	/// <summary>
	/// This function is called every fixed framerate frame, if the MonoBehaviour is enabled.
	/// </summary>
	void FixedUpdate()
	{
		
	}
	
	// Update is called once per frame
	void Update () 
	{
		RecoverHp();
		SmallPlaneMove();
	}

	/// <summary>
	/// LateUpdate is called every frame, if the Behaviour is enabled.
	/// It is called after all Update functions have been called.
	/// </summary>
	void LateUpdate()
	{
		
	}

	private void InitPro()
	{
		type = EnemyPlane.Type_SmallEnemyPlane;
		hp = 100;
		currHp = hp;
		defendPower = 5;
		defendHP = 0;
		recoverHP = 0;
		speed = 5;
		resistanceLaser = 1;
		resistanceMissile = 1;
		resistanceBullet = 1;
		startTrans = this.transform;

	}

	// 设置造船技术力并且重置属性
	public void setProducePower(int producePower)
	{
		this.producePower = producePower;
		ResetProByProducePower();
	}

	private void ResetProByProducePower()
	{
		/*
		敌机属性根据producePower变化而变化
		 */
	}


	private void SmallPlaneMove()
	{
		if(target != null && target.transform.position.z == 0)
		{
			timer = 0;
			distanceTarget = Vector3.Distance(transform.position, target.transform.position);
			targetEndPosition = target.transform.position;
			// 星球攻击状态
			if(star.state == Star.state_attack)
			{
				RotateMove(target.transform.position);
			}
			// 星球防御状态
			else if(star.state == Star.state_defend)
			{
				// 两倍攻击范围内追踪
				if(distanceTarget < distanceWeaponAttack * 2)
				{
					RotateMove(target.transform.position);
				}
				// 大于两倍范围飞回原位
				if(distanceTarget > distanceWeaponAttack * 2)
				{
					target = null;
				}
			}
			// 星球死亡状态
			else if(star.state == Star.state_death)
			{
				// 全图追踪
				if(distanceTarget > distanceWeaponAttack)
				{
					transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
				}
			}
		}
		else
		{
			timer += Time.deltaTime;
			if(timer < 3)
			{
				RotateMove(targetEndPosition);
			}
			else
			{
				// transform.up = (star.transform.position - transform.position).normalized;
				// transform.position = Vector3.MoveTowards(transform.position, star.transform.position, speed * Time.deltaTime);
				RotateMove(star.transform.position);
			}
			
		}
	}


	private void RotateMove(Vector3 position)
	{
		float dx = position.x - this.transform.position.x;
		float dy = position.y - this.transform.position.y;
		float rotationZ = Mathf.Atan2(dy, dx) * Mathf.Rad2Deg;

		//得到最终的角度并且确保在 [0, 360) 这个区间内     
        	rotationZ -= 90;
        	rotationZ = MakeSureRightRotation(rotationZ);

        	//获取增加的角度
        	float originRotationZ = MakeSureRightRotation(this.transform.eulerAngles.z);
        	// float addRotationZ = rotationZ - originRotationZ;
			float addRotationZ = MakeSureRightRotation(rotationZ - originRotationZ);

        	//超过 180 度需要修改为负方向的角度
        	if (addRotationZ > 180)
        	{	
            	addRotationZ -= 360;
        	}
			
			

        	//不超过每帧最大可旋转的阀值
        	addRotationZ = Mathf.Max(-max_rotation, Mathf.Min(max_rotation, addRotationZ));
			
			//应用旋转
        	this.transform.eulerAngles = new Vector3(0, 0, this.transform.eulerAngles.z + addRotationZ);
        	//移动
        	this.transform.Translate(new Vector3(0,speed * Time.deltaTime, 0));
	}


	private float MakeSureRightRotation(float rotation)
    {
        rotation += 360;
        rotation %= 360;
        return rotation;
    }








	public void TakeDamage(float damage){
		if(currHp <= 0)return;
		currHp -= damage;
		hpSlider.hpSlider.value = currHp / hp;
		if(currHp <= 0)
		{
//			print("一个小型敌机死啦");
			hpSlider.hpSlider.value = 0;
			/*
			播放死亡动画
			 */
			Destroy(gameObject);
		}
	}





	/// <summary>
	/// Sent when another object enters a trigger collider attached to this
	/// object (2D physics only).
	/// </summary>
	/// <param name="other">The other Collider2D involved in this collision.</param>
	void OnTriggerEnter2D(Collider2D other)
	{
		
	}
}
